﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace LiteExpress.Core.DbClient
{
    /// <summary>
    /// DataAccess异常基类
    /// </summary>
    [Serializable]
    public class DbClientException : Exception
    {
        /// <summary>
        /// 屏蔽用户名,密码
        /// </summary>
        /// <param name="connectionString"></param>
        /// <returns></returns>
        private static string HandleConnectionString(string connectionString)
        {
            string strResult = String.Empty;
            //1)屏蔽用户名
            Regex regex = new Regex(@";\s*(?:user|user id)\s*=([^;]*)", RegexOptions.IgnoreCase);
            Match match = regex.Match(connectionString);
            if (match.Success)
            {
                strResult = connectionString.Replace(match.Groups[1].Value, "******");
            }

            //2)屏蔽密码
            regex = new Regex(@";\s*password\s*=([^;]*)", RegexOptions.IgnoreCase);
            match = regex.Match(connectionString);
            if (match.Success)
            {
                strResult = strResult.Replace(match.Groups[1].Value, "******");
            }

            return strResult;
        }

        /// <summary>
        /// 用于序列化
        /// </summary>
        /// <param name="info"></param>
        /// <param name="context"></param>
        public DbClientException(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }

        /// <summary>
        /// 构造一个DataAccess异常基类
        /// </summary>
        /// <param name="message">异常消息</param>
        /// <param name="innerException">内部异常</param>
        public DbClientException(Exception innerException, string message)
            : base(String.Format("innerException:{0},external message:{1}", innerException.Message, message), innerException)
        {
        }
        
        /// <summary>
        /// 构造一个DataAccess异常基类
        /// </summary>
        /// <param name="innerException">内部异常</param>
        public DbClientException(Exception innerException)
            : base(innerException.Message, innerException)
        {
        }

        /// <summary>
        /// 构造一个DataAccess异常基类
        /// </summary>
        /// <param name="message">异常消息</param>
        public DbClientException(string message)
            : base(message, null)
        {
        }
    }
}
